package com.canal.collection.controller;

import com.canal.collection.service.DataCollectionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import static com.canal.collection.util.GraphVerifyCodeUtil.generateVerifyCode;
import static com.canal.collection.util.GraphVerifyCodeUtil.outputImage;

/**
 * @author yangbo
 * @date 2020-06-29 17:04
 */
@Controller
public class PageDisplayController {

    private static final String OPPO_CODE = "CLV0Vn";

    @Autowired
    private DataCollectionService dataCollectionService;

    /**
     * 返回落地页，绑定id
     * @param item
     * @param model
     * @return
     */
    @RequestMapping(value = "/channel/{item}")
    public String h5(@PathVariable String item,
                     @RequestParam(value = "xmly_materialId",required = false) String xmlyMaterial,
                     Model model) {
        Integer id = dataCollectionService.itemId(item);
        if(id == null) {
            return "404";
        }
        model.addAttribute("id",id);
        if(StringUtils.isNotBlank(xmlyMaterial)) {
            return "xmly";
        }
        if(OPPO_CODE.equals(item)) {
            return "oppo";
        }
        return "h5";
    }

    /**
     * 生成图形验证码
     * @param width
     * @param height
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping(value = "/gateway/code/w/{width}/h/{height}")
    public void imageCode(@PathVariable Integer width,
                          @PathVariable Integer height,
                          HttpServletRequest request,
                          HttpServletResponse response) throws IOException {
        OutputStream outputStream = response.getOutputStream();
        String verifyCode = generateVerifyCode(4);
        //将code缓存到session中，验证时需要
        HttpSession session = request.getSession();
        session.setAttribute("code",verifyCode);
        session.setMaxInactiveInterval(120);
        width = width != null ? width : 100;
        height = height != null ? height : 40;
        outputImage(width, height, outputStream, verifyCode);
    }

    /**
     * 下载页
     * @param apkUrl
     * @param model
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/gateway/download")
    public String download(@RequestParam(value = "apkUrl") String apkUrl,
                           Model model) throws UnsupportedEncodingException {
        apkUrl = URLDecoder.decode(apkUrl,"UTF-8");
        model.addAttribute("apkUrl",apkUrl);
        return "download";
    }
}
